package com.moji.controller;


import com.moji.common.base.ApiResult;
import com.moji.common.entity.MyPage;
import com.moji.entity.DataDictionary;
import com.moji.service.DataDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author mds.
 * @since 2023-04-10
 */
@RestController
@RequestMapping("/api/dataDictionary")
@Api(tags = "数据字典", value = "数据字典通用接口")
public class DataDictionaryController {
    @Resource
    private DataDictionaryService dataDictionaryService;


    /**
     *根据字典类型ID,查询数据字典列表
     * @param
     * @return
     */
    @GetMapping("/queryListIndex")
    @ApiOperation(value = "查询数据字典 类型列表（主 A）",response = MyPage.class)
    public ApiResult<List<DataDictionary>> queryListIndex(){
        List<DataDictionary>  list = dataDictionaryService.queryListIndex();
        return ApiResult.ok(list);
    }

    /**
     *根据字典类型ID,查询数据字典列表
     * @param id
     * @return
     */
    @GetMapping("/queryListById")
    @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "Long")
    @ApiOperation(value = "根据字典类型ID,查询数据字典字典值(没有录入时不报错 返回空数组)",response = MyPage.class)
    public ApiResult<List<DataDictionary>> queryListById(Long id){
        List<DataDictionary>  list = dataDictionaryService.queryListById(id);
        return ApiResult.ok(list);
    }

    /**
     * 根据字典类型NAME,查询数据字典列表
     * @param name
     * @return
     */
    @GetMapping("/queryListByName")
    @ApiImplicitParam(name = "name",value = "name",required = true,dataType = "String")
    @ApiOperation(value = "根据字典类型NAME,查询数据字典字典值(没有录入时不报错 返回空数组)",response = MyPage.class)
    public ApiResult<List<DataDictionary>> queryListByName(String name){
        List<DataDictionary>  list = dataDictionaryService.queryListByName(name);
        return ApiResult.ok(list);
    }


    /**
     * 根据字典类型NAME,查询数据字典列表
     * @param code
     * @return
     */
    @GetMapping("/queryListByCode")
    @ApiImplicitParam(name = "code",value = "code",required = true,dataType = "String")
    @ApiOperation(value = "根据字典类型Code,查询数据字典字典值(没有录入时不报错 返回空数组)",response = MyPage.class)
    public ApiResult<List<DataDictionary>> queryListByCode(String code){
        List<DataDictionary>  list = dataDictionaryService.queryListByCode(code);
        return ApiResult.ok(list);
    }


    /**
     * 添加数据字典类型（主 A）
     * @param dataDictionary
     * @return
     */
    @PostMapping("/addDataDictionaryA")
    @ApiOperation(value = "添加数据字典类型（主 A）",response = MyPage.class)
    public ApiResult<Boolean> addDataDictionaryA(@RequestBody DataDictionary dataDictionary){
        try{
            boolean b = dataDictionaryService.addDataDictionaryA(dataDictionary);
            return ApiResult.ok(b);
        }catch(Exception e){
            return ApiResult.failed(e.getMessage());
        }
    }

    /**
     * 修改数据字典类型（主 A）
     * @param dataDictionary
     * @return
     */
    @PostMapping("/queryListByName")
    @ApiOperation(value = "修改数据字典类型（主 A）",response = MyPage.class)
    public ApiResult<Boolean> updateDataDictionaryA(@RequestBody DataDictionary dataDictionary){
        try{
            boolean b = dataDictionaryService.updateDataDictionaryA(dataDictionary);
            return ApiResult.ok(b);
        }catch(Exception e){
            return ApiResult.failed(e.getMessage());
        }
    }

    /**
     *删除数据字典类型（主 A）
     * @param id
     * @return
     */
    @GetMapping("/delDataDictionaryA")
    @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "Long")
    @ApiOperation(value = "删除数据字典类型（主 A） - 前端提示语：删除可能影响一些单据的引用，确认删除？",response = MyPage.class)
    public ApiResult<Boolean> delDataDictionaryA(Long id){
        try{
            boolean b = dataDictionaryService.delDataDictionaryA(id);
            return ApiResult.ok(b);
        }catch(Exception e){
            return ApiResult.failed(e.getMessage());
        }
    }


    /**
     * 添加数据字典 字典值（明细 B）
     * @param dataDictionary
     * @return
     */
    @PostMapping("/addDataDictionaryB")
    @ApiOperation(value = "添加数据字典 字典值（明细 B）",response = MyPage.class)
    public ApiResult<Boolean> addDataDictionaryB(@RequestBody DataDictionary dataDictionary){
        try{
            boolean b = dataDictionaryService.addDataDictionaryB(dataDictionary);
            return ApiResult.ok(b);
        }catch(Exception e){
            return ApiResult.failed(e.getMessage());
        }
    }

    /**
     * 修改数据字典 字典值（明细 B）
     * @param dataDictionary
     * @return
     */
    @PostMapping("/updateDataDictionaryB")
    @ApiOperation(value = "修改数据字典 字典值（明细 B）",response = MyPage.class)
    public ApiResult<Boolean> updateDataDictionaryB(@RequestBody DataDictionary dataDictionary){
        try{
            boolean b = dataDictionaryService.updateDataDictionaryB(dataDictionary);
            return ApiResult.ok(b);
        }catch(Exception e){
            return ApiResult.failed(e.getMessage());
        }
    }

    /**
     * 删除数据字典 字典值（明细 B）
     * @param id
     * @return
     */
    @GetMapping("/delDataDictionaryB")
    @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "Long")
    @ApiOperation(value = "删除数据字典 字典值（明细 B）- 前端提示语：删除可能影响一些单据的引用，确认删除？",response = MyPage.class)
    public ApiResult<Boolean> delDataDictionaryB(Long id){
        try{
            boolean b = dataDictionaryService.delDataDictionaryB(id);
            return ApiResult.ok(b);
        }catch(Exception e){
            return ApiResult.failed(e.getMessage());
        }
    }

}

